import store from '../store/index.js'
// 登录中
// store.commit('updataToken',res.data.token);
// 					store.commit('updataUserId',res.data.wx_user.id);
// 					store.commit('updataPhone',res.data.wx_user.phone);
// 					store.commit('updataCanJin',res.data.wx_user.is_can_jin)
// 					// 将用户的id保存在本地存储
// 					uni.setStorageSync("userId",res.data.wx_user.id)
// 					uni.setStorageSync("phone",res.data.wx_user.phone)
function userLogin(share_id,fromJin,redirect_url=location.href,arg={}){
	return  new Promise((resolve, reject) => {
			if(!store.getters.getToken){
				arg.redirect_url=encodeURI(redirect_url)
				if(share_id){
					arg.share_id=share_id
				}
				if(fromJin){
					arg.fromJin=fromJin
				}
				let str_arg=convertObj(arg)
				window.location.href=store.state.URL+"wechat/official_wechat/login?"+str_arg	
			}
			resolve()
		}).then(login).then(function(re){
			console.log(re)
		})
}
function convertObj(data) {
    var _result = [];
    for (var key in data) {
      var value = data[key];
      if (value.constructor == Array) {
        value.forEach(function(_value) {
          _result.push(key + "=" + _value);
        });
      } else {
        _result.push(key + '=' + value);
      }
    }
    return _result.join('&');
}
// 登录中
// async function login(){
// 	await _login()
// }
function  login(){
	return  new Promise((resolve, reject) => {
		let self = this
		let token=store.state.token
		uni.request({
			url:store.state.URL + 'user/api_index/info',
			data:{
				token: token,
			},
			method:"POST",
			success(res) {
				console.log('用户登录后的数据',res.data.data)
				if(res.data.status) {
					store.commit('updataUserId',res.data.data.user.user_id);
					store.commit('updataPhone',res.data.data.user.phone);
					store.commit('updataCanJin',res.data.data.is_captain)
					store.commit('updataUserInfo',res.data.data.user);
					uni.setStorageSync("userInfo",res.data.data.user)
					// 将用户的id保存在本地存储
					uni.setStorageSync("userId",res.data.data.user.user_id)
					uni.setStorageSync("phone",res.data.data.user.phone)
				}
				resolve(res.data)
			}
		})
	})
	
}
// 获取微信步数
function userWerun(){
	uni.login({
		provider:"weixin",
		success(res) {
			werunCode = res.code
			uni.authorize({
			    scope: 'scope.werun',
			    success(res) {
					console.log('获取微信步数授权success',res)
					wx.getWeRunData({
						success(res) {
							console.log('获取步数数据success',res)
							werunIv = res.iv
							werunEncryptedData = res.encryptedData
							testWerun()
						},
						fail(err) {
							console.log('获取步数数据err',err)
						}
					})
			    },
				fail(err) {
					console.log('获取微信步数授权err',err)
				}
			})
		}
	})
}
function testWerun(){
	uni.request({
		url:store.state.URL + 'login/login/mini_program_get_steps',
		data:{
			code: werunCode,
			iv: werunIv,
			encryptedData: werunEncryptedData,
			token: store.getters.getToken
		},
		success(res) {
			console.log(res)
			if(res.data.status&&res.data.msg=='获取成功') {
				console.log('获取用户的步数成功',res)
				store.commit('updataCanStep',true)
			}else {
				store.commit('updataCanStep',false)
			}
		},
		fail(err) {
			console.log('获取用户的步数失败',err)
		}
	})
}
// 检查是否已授权
function testGet(){
	if(!res.authSetting['scope.userInfo']){
		newInfo()
		uni.showToast({
			title:"请先登录",
			icon:"none",
			duration:2000
		})
	}else{
		console.log('11111111111111')
	}
}
module.exports = {
	userLogin: userLogin,
	testGet: testGet,
	login: login,
	userWerun: userWerun
}